#pragma once
#include <stdio.h>
#include "stdafx.h" 
#include "tstack.h"

template <typename  TElem>
class TStackSqu :public TStack<TElem>
{
protected:
	
	TElem *room;
	TElem elembuff;
	long size;
	long CopyRoom(TElem *objRoom,long n1,TElem *srcRoom,long n2)
	{
		long i,k;
		k=n2;
		if(k>n1)k=n1;
		for(i=0;i<k;i++)
			objRoom[i]=srcRoom[i];
		return k;
	};
	long ResizeRoom(long newSize)
	{
		long i,k;
		TElem *pE;
		pE=new(nothrow) TElem[newSize];
		if(pE==NULL);
		len=CopyRoom(pE,newSize,room,len);
		if(room!=NULL)delete room;
		room=pE;
		size=newSize;
		return len;
	};
public:
	TStackSqu(void){
		len=0;
	    room=new(nothrow) TElem[100];
	    size=0;
	};
	TStackSqu(long mSize){
		len=0;
		room=new(nothrow) TElem[mSize];
		size=mSize;
	};
	~TStackSqu(void){
	  if(room!=NULL)delete[]room;
	};
	virtual void Print(){};
	virtual TElem& Push(TElem &elem)
	{
		if(len>=size-1)
		{
			long ret=this->ResizeRoom(size+10);
			if(ret<0);
		}
		room[len]=elem;
		
		len++;
		printf("push:%d\n",len);
		return room[len-1];
	}

	virtual TElem& Pop(void)
	{
		if(len<=0);
		len--;
		return room[len];
	}


	virtual TElem& GetTop(void)
	{
	   return room[len-1];
	}

	virtual void Clear()
	{
		len=0;
	}


	virtual TElem& Rolldown()
	{
	   TElem x;
	   x=room[0];
	   for(long i=1;i<len;i++)
		   room[i-1]=room[i];
	   room[len-1]=x;
	   return room[len-1];
	}

	virtual TElem& RollUp()
	{
	   TElem x;
	   x=room[len-1];
	   for(long i=0;i<len;i++)
		   room[i+1]=room[i];
	   room[0]=x;
	   return room[0];
	}


};




